package com.westvalley.syn.bean;

public class Result {
	private boolean flag;				//结果标识
	private String msg;					//错误信息
	private int successNum;				//同步成功总数
	private int failNum;				//同步失败总数
	private int interfaceDataNum;		//接口数据总数
	private int insertNum;				//新增数据总数
	private int updateNum;				//更新数据总数
	
	private Result(){}
	
	private Result(boolean flag,String msg,int successNum,int failNum,int interfaceDataNum,int insertNum,int updateNum){
		parse(flag,msg,successNum,failNum,interfaceDataNum,insertNum,updateNum);
	}
	
	private Result(int interfaceDataNum,int[] insertNums,int[] updateNums){
		int insertSuccessNum = getSqlSuccessNum(insertNums,-2);
		int updateSuccessNum = getSqlSuccessNum(updateNums,-2);
		int successNum = (insertSuccessNum+updateSuccessNum);
		int failNum = interfaceDataNum-(insertSuccessNum+updateSuccessNum);
		boolean flag = failNum==0?true:false;
		String msg = "操作失败"+failNum+"条记录!";
		parse(flag,msg,successNum,failNum,interfaceDataNum,insertSuccessNum,updateSuccessNum);
	}
	
	private Result(Exception e){
		parse(false,e.getMessage(),0,0,0,0,0);
	}
	
	private void parse(boolean flag,String msg,int successNum,int failNum,int interfaceDataNum,int insertNum,int updateNum){
		this.flag = flag;
		this.msg = msg;
		this.successNum = successNum;
		this.failNum = failNum;
		this.interfaceDataNum = interfaceDataNum;
		this.insertNum = insertNum;
		this.updateNum = updateNum;
	}
	
	private int getSqlSuccessNum(int[] intArr,int successValue){
		int successNum = 0;
		if(intArr!=null){
			for(int i : intArr){
				if( i==successValue ){
					successNum++;
				}
			}
		}
		return successNum;
	}
	
	public static Result me(){
		return new Result();
	}
	
	public static Result me(boolean flag,String msg,int successNum,int failNum,int interfaceDataNum,int insertNum,int updateNum){
		return new Result(flag,msg,successNum,failNum,interfaceDataNum,insertNum,updateNum);
	}
	
	public static Result me(int interfaceDataNum,int[] insertNums,int[] updateNums){
		return new Result(interfaceDataNum,insertNums,updateNums);
	}
	
	public static Result me(Exception e){
		return new Result(e);
	}
	
	public boolean isFlag() {
		return flag;
	}
	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public int getSuccessNum() {
		return successNum;
	}
	public void setSuccessNum(int successNum) {
		this.successNum = successNum;
	}
	public int getFailNum() {
		return failNum;
	}
	public void setFailNum(int failNum) {
		this.failNum = failNum;
	}
	public int getInterfaceDataNum() {
		return interfaceDataNum;
	}
	public void setInterfaceDataNum(int interfaceDataNum) {
		this.interfaceDataNum = interfaceDataNum;
	}
	public int getInsertNum() {
		return insertNum;
	}
	public void setInsertNum(int insertNum) {
		this.insertNum = insertNum;
	}
	public int getUpdateNum() {
		return updateNum;
	}
	public void setUpdateNum(int updateNum) {
		this.updateNum = updateNum;
	}
	
}
